home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12313 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Bad code
  5. Date: Sun, 31 Mar 96 12:21:15 GMT
  6. Organization: none
  7. Message-ID: <828274875snz@genesis.demon.co.uk>
  8. References: <4jhau8$hrl@hermes.oanet.com> <4jhmhpINNps7@anvil.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jhmhpINNps7@anvil.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >In article <4jhau8$hrl@hermes.oanet.com>,
  18. > <scorpion@portal.connect.ab.ca> wrote:
  19. >>       I need to know why a piece of my C code doesn't work . 
  20. >>it does like:
  21. >>
  22. >>       Object.Vertices+i.X = (long) .....
  23. >>       Object.Vertices+i.Y = (long).......
  24. >>       Object.Vertices+i.Z=(long) ........
  25. >>
  26. >>       Vertices is a pointer to a struct with {long X,Y,Z} that 
  27. >>has been typedef'ed. i is an index vatiable that is added to the 
  28. >>pointer to make is point to the next {long X,Y,Z} typedef'ed 
  29. >>struct . When I go to compile , it says "not a struct or union 
  30. >>type".
  31. >
  32. >Object.Vertices is NOT a struct or union type, so you can't use the '.'
  33. >operator to access members in it. It is a pointer to a structure. Therefore you
  34. >have to dereference the pointer before accessing a member:
  35. >
  36. >        (*Object.Vertices+i).x = (long) ....
  37.  
  38. Or rather:
  39.  
  40.          (*(Object.Vertices+i)).X = (long) ....
  41.  
  42. >This is a bit clumsy, which is why most programmers use the following
  43. >equivalent notation:
  44. >
  45. >        (Object.Vertices+i)->X = ...
  46.  
  47. More commonly this is written as:
  48.  
  49.          Object.Vertices[i].X = ...
  50.  
  51. >When I parenthesize Vertices+i , I get an "Identified 
  52. >
  53. >That's because (Vertices+i) is not a member of Object structure. It is a
  54. >pointer expression.
  55.  
  56. Not even that. Vertices is just a member name and by itself is not an
  57. expression at all, and neither is Vertices+i. Vertices can only form part
  58. of an expression as the right hand operand of . or -> or argument to
  59. offsetof().
  60.  
  61. You cannot write Object.(Vertices + i). An identifier is
  62. >expected after the ``Object.'' to name the member.
  63.  
  64. Right, here Vertices is the lefthand operand of + so this is not valid.
  65.  
  66. -- 
  67. -----------------------------------------
  68. Lawrence Kirby | fred@genesis.demon.co.uk
  69. Wilts, England | 70734.126@compuserve.com
  70. -----------------------------------------
  71.